package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPreview.aibkPlayerSpeed

import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import kotlinx.android.synthetic.main.aibk_adapter_item_speed_choice.view.*

class PlayerSpeedChoiceAdapter(
        val activity: AppCompatActivity,
        var audioSpeed: Double,
        val index_A: Int,
        val index_B: Int
) : KBaseRecyclerAdapter() {
    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_speed_choice, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        if(position == 0){
            holder.convertView.TvPlayerSpeedItem.text = "0.8x"
            if(audioSpeed == 0.8){
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
            }else{
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.textColor2));
            }
        }else if(position == 1){
            holder.convertView.TvPlayerSpeedItem.text = "0.9x"
            if(audioSpeed == 0.9){
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
            }else{
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.textColor2));
            }
        }else if(position == 2){
            holder.convertView.TvPlayerSpeedItem.text = "1.0x"
            if(audioSpeed == 1.0){
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
            }else{
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.textColor2));
            }
        }else if(position == 3){
            holder.convertView.TvPlayerSpeedItem.text = "1.1x"
            if(audioSpeed == 1.1){
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
            }else{
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.textColor2));
            }
        }else if(position == 4){
            holder.convertView.TvPlayerSpeedItem.text = "1.2x"
            if(audioSpeed == 1.2){
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
            }else{
                holder.convertView.TvPlayerSpeedItem.setTextColor(activity.resources.getColor(R.color.textColor2));
            }
        }
        holder.convertView.TvPlayerSpeedItem.setOnClickListener {
            if(position == 0){
                audioSpeed = 0.8
            }else if(position == 1){
                audioSpeed = 0.9
            }else if(position == 2){
                audioSpeed = 1.0
            }else if(position == 3){
                audioSpeed = 1.1
            }else if(position == 4){
                audioSpeed = 1.2
            }

            notifyDataSetChanged()
            Handler().postDelayed({
                clickListener!!.onClick(audioSpeed)
            },100)
        }
    }

    override fun getItemCount(): Int {
        return 5
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    interface OnClickListener {
        fun onClick(playerSpeed:Double)
    }

    private var clickListener: OnClickListener? = null

    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

}